package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.entity.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author kcs
 * @date 2021/11/22
 * @TIME:21:13
 */
@Slf4j
@RestController
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    @PostMapping("/checkgroup/add.do")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        //数据校验
        if(checkGroup == null || checkitemIds.length <= 0){
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);//参数非法
        }

        //判断该检查组是否已存在
        LambdaQueryWrapper<CheckGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CheckGroup::getCode,checkGroup.getCode()).eq(CheckGroup::getName,checkGroup.getName());
        CheckGroup one = checkGroupService.getOne(wrapper);

        //如果存在,直接返回结果
        if(one != null){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL); //检查组已存在
        }

        //不存在,添加检查组
        checkGroupService.save(checkGroup);

        //获取新添加检查组id
        LambdaQueryWrapper<CheckGroup> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CheckGroup::getCode,checkGroup.getCode());
        CheckGroup checkGroup1 = checkGroupService.getOne(wrapper1);
        Integer checkGroupId = checkGroup1.getId();

        //在关系表中添加该检查组和对应检查项的关系
        for (Integer checkitemId : checkitemIds) {
            checkGroupService.add(checkitemId,checkGroupId);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);//新增检查组成功
    }

    /**
     * 分页条件查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/checkgroup/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        //数据校验
        if(queryPageBean == null){
            return null;//参数非法
        }

        //设置分页查询条件
        IPage<CheckGroup> iPage = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        LambdaQueryWrapper<CheckGroup> wrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        wrapper.like(queryString != null,CheckGroup::getCode,queryString)
                .or()
                .like(queryString != null,CheckGroup::getName,queryString)
                .or()
                .like(queryString != null,CheckGroup::getHelpCode,queryString);
        checkGroupService.page(iPage,wrapper);

        //如果查询页码大于总页数时,设置当前页码为总页数
        if(iPage.getCurrent() > iPage.getPages()){
            iPage.setCurrent(iPage.getPages());
            checkGroupService.page(iPage,wrapper);
        }
        return new PageResult(iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 根据id查询检查组 -- 回显
     * @param id
     * @return
     */
    @GetMapping("/checkgroup/findById.do")
    public Result findById(Integer id){

        //数据校验
        if(id == null){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);//参数非法
        }

        //查询并返回结果
        CheckGroup one = checkGroupService.getById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,one);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @return
     */
    @PostMapping("/checkgroup/edit.do")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        //数据校验
        if(checkGroup == null || checkitemIds.length <= 0){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);//参数非法
        }

        //修改并返回结果
        checkGroupService.updateById(checkGroup);

        //在关系表中删除该检查组和对应检查项的关系数据
        checkGroupService.delete(checkGroup.getId());

        //在关系表中添加更新后的该检查组和对应检查项的关系
        for (Integer checkitemId : checkitemIds) {
            checkGroupService.add(checkitemId,checkGroup.getId());
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据检查组ID查询检查组包含的多个检查项ID
     * @param id
     * @return
     */
    @GetMapping("/checkgroup/findCheckItemIdsByCheckGroupId.do")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        //数据校验
        //获取该id对应检查组包含的多个检查项ID
        int[] ids = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,"查询成功",ids);
    }

    /**
     * 新增套餐时回显检查组信息
     * @return
     */
    @GetMapping("/checkgroup/findAll.do")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.list();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
